package com.example.newsmanager.controller;


import com.example.newsmanager.common.Result;
import com.example.newsmanager.entity.Comment;
import com.example.newsmanager.entity.DTO.CommentDTO;
import com.example.newsmanager.entity.User;
import com.example.newsmanager.service.CommentService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;
    @GetMapping("/parent")
    private Result<List<Comment>> getCommentByParentId(@RequestParam(value = "parentId") Long parentId){
        return Result.success(commentService.getCommentByParentId(parentId),"通过父评论查询回复成功");
    }
    @GetMapping("/list")
    private Result<List<Comment>> getList(){
        return Result.success(commentService.getList(),"查询父评论成功");
    }
    @GetMapping("/id")
    private Result<Comment> getById(@RequestParam(value = "id")Long id){
        return Result.success(commentService.getById(id),"查询单个评论成功");
    }
    @PostMapping("/add")
    private Result<?> addComment(@RequestBody Comment comment, HttpServletRequest request){
        User currentUser = (User) request.getAttribute("currentUser");
        if(currentUser == null){
            return Result.fail("登录状态失效");
        }
        comment.setUserName(currentUser.getNickname());
        comment.setUserAvatar(currentUser.getAvatar());
        commentService.addComment(comment);
        return Result.success(comment,"添加评论成功成功");
    }
    @GetMapping("/all")
    private Result<List<CommentDTO>> getAllComments(){
        return Result.success(commentService.getAllComments(),"查询全部评论成功");
    }
    @GetMapping("/newsid")
    private Result<List<CommentDTO>> getAllCommentsByNewsId(@RequestParam(value = "id")Long id){
        return Result.success(commentService.getAllCommentsByNewsId(id),"获取当前新闻评论区成功");
    }
    @DeleteMapping("/delete")
    private Result<?> deleteComment(@RequestParam(value = "commentId") Long commentId){
        commentService.deleteComment(commentId);
        return Result.success("删除评论成功");
    }
}

